<template>
  <div class="app">
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="电键" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.docEntry" placeholder="电键" />
        </a-form-item>
        <a-form-item class="smart-query-form-item">
          <a-button-group>
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
          </a-button-group>
        </a-form-item>
      </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
      <!---------- 表格操作行 begin ----------->
      <a-row class="smart-table-btn-block">
        <a-button-group>
          <a-button @click="add" type="primary">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button>
          <a-button @click="() => (data.importModalShowFlag = true)" type="primary">
            <template #icon>
              <ExportOutlined />
            </template>
            导入
          </a-button>

          <a-button @click="OnExport" type="primary">
            <template #icon>
              <ImportOutlined />
            </template>
            导出
          </a-button>
        </a-button-group>

        <div class="smart-table-setting-block">
          <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
        </div>
      </a-row>
      <!---------- 表格操作行 end ----------->

      <!---------- 表格 begin ----------->
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="docEntry" :loading="tableLoading" :pagination="false">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'docEntry'">
            <a @click="onTo(text)">{{ text }} </a>
          </template>
        </template>
      </a-table>
      <!---------- 表格 end ----------->

      <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
        />
      </div>
    </a-card>
  </div>

  <a-modal v-model:open="data.importModalShowFlag" title="导入" @onCancel="HideImportModal" @ok="HideImportModal">


      <a-upload
        v-model:fileList="data.fileList"
        name="file"
        :multiple="false"
        action="http://msg.xacjoe.com:10000/questions/import"
        accept=".xls,.xlsx"
        :before-upload="BeforeUpload"
        @remove="HandleRemove"
        :headers="{ Authorization: 'Bearer ' + useUserStore().getToken }"

      >
        <a-button>
          <upload-outlined />
          选择文件
        </a-button>
      </a-upload>

  </a-modal>
</template>
<script setup>
  import { reactive, ref, onMounted } from 'vue';
  import { questionsApi } from '/src/api/business/exam/questions-api.js';
  import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const';
  import { smartSentry } from '/src/lib/smart-sentry';
  import TableOperator from '/src/components/support/table-operator/index.vue';
  import { useRouter } from 'vue-router';
  import { columns } from '/@/views/business/exam/questionData/data.js';
  import {useUserStore} from "/@/store/modules/system/user.js";

  const router = useRouter();

  const queryForm = reactive({
    docEntry: undefined, //电键
    pageNum: 1,
    pageSize: 20,
  });

  const data = reactive({
    importModalShowFlag: false,
    fileList: [],
  });

  // 表格加载loading
  const tableLoading = ref(false);
  // 表格数据
  const tableData = ref([]);
  // 总数
  const total = ref(0);

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize;

    queryForm.pageSize = pageSize;
    queryData();
  }

  // 搜索
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true;
    try {
      let queryResult = await questionsApi.queryPage(queryForm);
      tableData.value = queryResult.data.list;
      total.value = queryResult.data.total;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  function onTo(item) {
    router.push({ path: 'questiona', query: { type: 'info', docEntry: item } });
  }

  function add() {
    router.push({ path: 'questiona', query: { type: 'new', docEntry: null } });
  }

  function OnExport() {
    questionsApi.export(queryForm);
  }

  function HideImportModal() {
    queryData()

  }

  function HandleRemove() {}

  function BeforeUpload() {}


  onMounted(queryData);
</script>
